/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.test.redis;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.tianyun.cloud.data.redis.factory.ObjectRedisTemplate;
import org.tianyun.cloud.test.BaseTestCase;

/**
 * Redis测试用例
 *
 * @auther ebert_chan
 */
public class RedisTestCase extends BaseTestCase {

    @Autowired
    private ObjectRedisTemplate redisOperateTemplate;

    @Test
    public void testIncrement() throws InterruptedException {

        String key = "test:increment";

        redisOperateTemplate.delete(key);

        Long value = redisOperateTemplate.increment(key, 2);
        assert value == 1;

        value = redisOperateTemplate.increment(key, 2);
        assert value == 2;

        String text = redisOperateTemplate.get(key);
        assert text != null;

        Thread.sleep(2000);

        text = redisOperateTemplate.get(key);
        assert text == null;

    }

    @Test
    public void testLock() throws InterruptedException {

        String key = "test:lock", value = "1";

        redisOperateTemplate.delete(key);

        boolean isLock = redisOperateTemplate.tryLock(key, value, 3000);
        assert isLock;

        isLock = redisOperateTemplate.tryLock(key, value, 3000, 1000);
        assert !isLock;

        isLock = redisOperateTemplate.tryLock(key, value, 3000, 5000);
        assert isLock;

        assert redisOperateTemplate.unlock(key, value);

    }

}
